﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;

namespace IHISCETeim.Web.MachineSys.UserModel
{
    public partial class UWarrantyCardEdit : PageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.修改 };
            }
        }
        protected override string DWZRef
        {
            get { return "UWarrantyCardList";}
        }

        #region
        /// <summary>
        /// 需要修改的ID
        /// </summary>
        protected int UWCID
        {
            get
            {
                return GetRequestValue<int>("UWCID");
            }
        }

        private UWarrantyCardInfo _obj;
        protected UWarrantyCardInfo obj
        {
            get
            {
                if (_obj == null)
                    _obj = UWarrantyCardController.GetByUWCID(UWCID);
                return _obj;
                //OMenuController.ge
            }
        }

        private List<UQuestionnaireInfo> _listuq;
        protected List<UQuestionnaireInfo> listUQ
        {
            get
            {
                if (_listuq == null)
                    _listuq = UWarrantyCardController.GetUQuestionnaireByUWCID(UWCID);
                return _listuq;
                //OMenuController.ge
            }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (GetRequestValue<string>("ajaxmethod", "") == "edit")
            {
                addmenu();
                
            }
        }

        protected void addmenu()
        {
            //OJobInfo obj = Request.SerializableObject<OJobInfo>();
            // obj.UserType = 1;
            UWarrantyCardInfo objUWC = Request.SerializableObject<UWarrantyCardInfo>();
            //objUWC.CustomerAddress = string.Format("{0}省{1}市{2}县(街){3}乡(镇){4}村", objUWC.CustomerAddress.Split(new char[] { ',' }));
            objUWC.AgentName = GetRequestValue<string>("dwz.orgLookup.AgentName", "");
            objUWC.AgentAddress = GetRequestValue<string>("dwz.orgLookup.AgentAddress", "");

            //objUWC.CustomerAddress = "";//string.Format("{0}省{1}市{2}县(街){3}乡(镇){4}村", objUWC.CustomerAddress.Split(new char[]{ ','}));
            //string[] caddresss = objUWC.CustomerAddress.Split(new char[] { ',' });
            //string[] caddresssuff = new string[] { "省", "市", ",县(街)", "乡(镇)", "村" };
            //int i = 0;
            //foreach (string item in caddresss)
            //{
            //    if (!string.IsNullOrEmpty(item))
            //        objUWC.CustomerAddress += item + caddresssuff[i];
            //    i++;
            //}

            UWarrantyCardController.Update(objUWC,obj);

            List<UQuestionTempleteInfo> allQtemp = UWarrantyCardController.GetAllUQuestionTempleteByParId(0);

            List<UQuestionnaireInfo> alluq = new List<UQuestionnaireInfo>();
            foreach (UQuestionTempleteInfo item in allQtemp)
            {
                string[] xx = GetRequestValue<string>("uqt" + item.UQTID.ToString(), "").Split(new char[] { ',' });
                foreach (string sitem in xx)
                {
                    if (sitem.IsEmptyOrNull())
                        continue;

                    UQuestionnaireInfo uqtitem = new UQuestionnaireInfo();
                    uqtitem.UQTID = Convert.ToInt32(sitem);
                    uqtitem.UWCID = objUWC.UWCID;
                    uqtitem.SerialNumber = objUWC.SerialNumber;
                    uqtitem.InputValue = "";
                    alluq.Add(uqtitem);
                }
            }
            UWarrantyCardController.AddUQuestionnaire(alluq);

            AddLog(Permission.修改,obj, objUWC);

            SuccessJson("成功修改", this.DWZRef);
            //ODailyInfo obj = Request.SerializableObject<ODailyInfo>();

            ////if (OUserController.CheckUserName(obj.UserName))
            ////{
            ////    AlertJson("登陆帐户名已存在,请重新输入.");
            ////    return;
            ////}
            //obj.JobCode = obj.JobCode.ToUpper();
            //List<OJobContentInfo> allJobCon = OJobController.GetALLJobContent();
            //OJobContentInfo tempobj = allJobCon.First<OJobContentInfo>(e => e.JobCode == obj.JobCode);
            //obj.JobTitle = tempobj.JobTitle;


            //if (ODailyController.Update(obj))
            //    SuccessJson("修改成功");
            //else
            //    ErrorJson("修改失败");
        }

        protected string GetIntoSpecial(string item)
        {
           string[] allcheck = obj.IntoSpecial.Split(new char[] { ',' });
           foreach (string i in allcheck)
           {
               if (i == item)
               {
                   return "checked";
                   break;
               }
           }
           return "";
        }

        protected string GetIntoConditi(string item)
        {
            string[] allcheck = obj.IntoConditi.Split(new char[] { ',' });
            foreach (string i in allcheck)
            {
                if (i == item)
                {
                    return "checked";
                    break;
                }
            }
            return "";
        }

        #region 获取调查表模板
        protected string GetUQuestionTemplete()
        {
            string TempHtml = "";
           // TempHtml = UWarrantyCardController.DC.Get<string>(DataCacheKey.UQuestionTempleteDCKey);
          //  if (!TempHtml.IsEmptyOrNull())
           //     return TempHtml;
            TempHtml = "";
            string ItemTmpHtmlTitleTemp = "<b>({0}){1}</b>";
            string ItemTmpHtmlConTemp = "<table>{0}</table><div class=\"divider\"></div> ";
            List<UQuestionTempleteInfo> allQtemp = UWarrantyCardController.GetAllUQuestionTempleteByParId(0);
            foreach (UQuestionTempleteInfo item in allQtemp)
            {
                TempHtml = TempHtml + string.Format(ItemTmpHtmlTitleTemp, item.OrderId, item.Title); ;
                List<UQuestionTempleteInfo> AllitemTemp = UWarrantyCardController.GetAllUQuestionTempleteByParId(item.UQTID);
                string trhtml = "";
                int tdnum = 0;
                // item.RowTDNum = item.RowTDNum+1;
                trhtml = "<tr>";
                foreach (UQuestionTempleteInfo item2 in AllitemTemp)
                {
                    if ((tdnum % item.RowTDNum) == 0 && tdnum != 0)
                    {
                        trhtml = trhtml + "</tr><tr>";
                    }

                    string checkedstr= listUQ.Find(e => e.UQTID == item2.UQTID)==null?"":"checked";

                    trhtml = trhtml + string.Format("<td><label><input type=\"checkbox\" name=\"uqt{0}\" value=\"{1}\" {3}>{2}</label></td>", item.UQTID, item2.UQTID, item2.Title, checkedstr);
                    tdnum++;
                }
                trhtml = trhtml + "</tr>";

                TempHtml = TempHtml + string.Format(ItemTmpHtmlConTemp, trhtml);
            }

            //UWarrantyCardController.DC.Add(DataCacheKey.UQuestionTempleteDCKey, TempHtml);
            return TempHtml;
        }
        #endregion
    }
}